<template>
	<view>
		<view class="head-box">
			<view class="head-item" :class="index == indexs?'red' : ''"  v-for="(item,index) in topList " :key="item.name" @tap="colorEvent(index)">{{item.name}}</view>
		</view>
		<view class="order-content">
			<view class="order-item" v-for="item in newList " :key="item.order.id">
				<view class="order-text1">订单编号:{{item.order.orderSn}} <text  class="order-text2">{{item.order.status |statusText }}</text></view>
				<view class="order-line"></view>
				<view class="order-item2" v-for="ele in item.items" :key="ele.id">
					<image :src="ele.productPic"></image>
					<view class="item2text1">
						<view>	{{ele.productName}}</view>
				<view>编号:{{ele.id}}</view>
				<view>规格:</view>
					</view>
					<view  class="item2text2">
						<view>{{ele.productPrice}}元</view>
						<view>x{{ele.productQuantity}}</view>
					</view>
					<view  class="order-time">
						<view>时间:{{ele.createTime}}</view>
						<view>合计:{{ele.productPrice}}元</view>
					</view>
					<view class="order-bottom" v-if="indexs == 0">
						<button type="default" size="mini" @tap="offEvent(ele.orderId)">取消订单</button>
						<button type="default" size="mini" @tap="pay(item.order.id)">付款</button>
					</view>
					<view class="order-bottom" v-if="indexs == 2">
						<button type="default" size="mini">确认收货</button>
					</view>
					<view class="order-bottom" v-if="indexs == 3 && item.order.status == 4 ">
						<button type="default" size="mini">删除订单</button>
					</view>
				</view>
			</view>
			
		</view>
		<!-- 尾巴 -->
	</view>
</template>

<script>
	import{
		findAllOrders,
		orderReturnApplys,
		cancelOrder
	}from "@/api/user/userOrder/index.js"
	import{
		addPreOrder
	}from "@/api/cart/index.js"
	export default {
		data() {
			return {
				arrs:[],
				
				
				orderList:[],
				indexs:0,
				topList:[{
					name:"待付款"
				},
				{
					name:"已付款"
				},
				{
					name:"已发货"
				},
				{
					name:"已收货"
				},
				{
					name:"申请退款"
				}]
			};
		},
		methods:{
			// 点击变色
			colorEvent(val){
				this.indexs = val
			},
			// 取消订单
			offEvent(ids){
				// ids是传过来的 orderId ,打印出来没有问题
				console.log(ids)
				 cancelOrder(ids)
			 .then(res =>{
				 console.log(res)
				 this.init()
			 })
			
			},
			init(){
				findAllOrders()
				.then(res =>{
					console.log(res)
					this.orderList =res.data.data.orderList
					
					
					
				})
			},
			pay(id){
				console.log(id)
				uni.navigateTo({
					url:`/pages/home/cart/order/order?id=${id}`
				})
				// this.obj???????
				// obj 是我创建的空对象,怎么把上面四个值放到空对象中去
				//
				// var arr =[];
				// this.arrs.push(val,val1,val2,val3)
				// this.arrs.forEach(ele=>{
				// 	var obj ={
				// 		cartId:ele.id,
				// 		productId:ele.productId,
				// 		productQuantity:ele.quantity,
				// 		productSkuId:ele.productSkuId
				// 	}
				// 	arr.push(obj);
				// })
				// console.log("提交订单",arr)
				// console.log(this.arrs)
				// addPreOrder({
				// 	orderItemList:this.arr
				// }).then(res=>{
				// 	console.log(res)
				// })
			}
		},
		 filters:{
		
		                statusText(val){
		                    if(val == 0){
		                        return "未付款"
		                    }else if(val == 1){
		                        return "确认付款,等待发货"
		                    }else if(val == 2){
		                        return "已发货"
		                    }else if(val == 3){
		                        return "已收货"
		                    }else if(val == 4){
		                        return "已完成"
		                    }else{
		                        return "无效订单"
		                    }
		
		                }
		            },
		onLoad(ele){
			// 先接收一下隔壁传过来的下标
			this.indexs = ele.index;
			this.init()
			// orderReturnApplys()
		},
		computed:{
			// offEvents(){
			// 	this.orderList.filter(ele =>ele.items)
			// },
			newList(){
				return this.orderList.filter(ele=>{
					if(this.indexs == 3 ){
						return ele.order.status == 3 || ele.order.status == 4
					}else{
						return  ele.order.status == this.indexs
					}
					
				});
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	// 头部
.head-box{
	display: flex;
	align-items: center;
	justify-content: space-evenly;
	background-color: #354E44;
	color: #FFFFFF;
	.head-item{
		height: 40px;
		line-height: 40px;
	}
}
.red{
	color:red;
}
// 内容展示区
.order-content{
	background-color: #F1ECE7;
	
	.order-item{
		padding: 20px 20px;
		background-color: #FFFFFF;
		
		.order-text1{
			font-size: 12px;
		}
		.order-line{
			width: 90%;
			height: 2px;
			background-color: #F1ECE7;
			margin: 20px auto;
		}
		.order-text2{
			font-size: 16px;
			margin-left: 90px;
		}
	}
	.order-item2{
		display: flex;
		justify-content: space-between;
		padding: 20px 0;
		flex-wrap: wrap;
		position: relative;
		.item2text1{
			font-size: 12px;
			line-height: 30px;
			 white-space:normal
		}
		.item2text2{
			line-height: 30px;
		}
		image{
			display: block;
			width: 80px;
			height: 80px;
		}
		.order-time{
			display: flex;
			justify-content: space-between;
			width: 100%;
			padding-top: 20px;
		}
		.order-bottom{
			
			position: absolute;
			right: 0;
			bottom: -30px;
			button{
				margin-left: 20px;
			}
		}
	}
}
</style>
